package controller

import (
	"digital/server/common"
	"digital/server/model"
	"digital/server/response"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"strconv"
)


func GetClubs(ctx *gin.Context) {
	Uid := ctx.Query("uid")
	uid, _ := strconv.Atoi(Uid)

	DB := common.GetDB()
	DB = DB.Where("Exist=?", 1)

	var clubs []model.Club
	err := DB.Find(&clubs).Error
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", err)
		return
	}
	// 获取社团图片
	db := common.GetDB()
	var images []string
	for _, club := range clubs {
		var tmp []model.CImage
		err1 := db.Where("Cid=? AND Main=?", club.Cid, 1).Find(&tmp).Error
		if err1 != nil {
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			log.Println("err1:", err1)
			return
		}
		for _, image := range tmp {
			images = append(images, image.Path) // 只添加路径字段
		}
	}

	//判断社团是否被收藏
	Db := common.GetDB()
	var collected []int
	for _, club := range clubs {
		var count int
		err2 := Db.Model(&model.ClubCollection{}).Where("Cid=? AND Uid =?", club.Cid, uid).Count(&count).Error
		if err2 != nil {
			response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
			log.Println("err2:", err2)
			return
		}
		if count > 0 {
			collected = append(collected, 1)
		} else {
			collected = append(collected, 0)
		}
	}

	// 返回查询结果
	response.Success(ctx, gin.H{"clubs": clubs, "images": images, "collected": collected}, "获取成功")
}

// 返回招新信息
func RecruitmentInfo(ctx *gin.Context) {
	// 查询数据库
	DB := common.GetDB()

	var notices []model.Notice
	err := DB.Where("recruitment=?", 1).Order("Ntime DESC").Limit(10).Find(&notices).Error
	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 400, nil, "发生错误，请重试")
		log.Println("err:", err)
		return
	}
	// 返回查询结果
	response.Success(ctx, gin.H{"notices": notices}, "获取成功")
}

// 收藏社团和取消收藏
func CollectClub(ctx *gin.Context) {
	// 获取用户ID和社团ID,并转换为int
	Uid := ctx.PostForm("uid")
	uid, err := strconv.Atoi(Uid)
	Cid := ctx.PostForm("cid")
	cid, err1 := strconv.Atoi(Cid)

	if err != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "警告！警告！")
		log.Println("err", err)
		return
	}

	if err1 != nil {
		response.Response(ctx, http.StatusBadRequest, 422, nil, "警告，警告！")
		return
	}

	// 获取收藏状态
	collected := ctx.PostForm("collected")

	DB := common.GetDB()
	if collected == "0" {
		// 创建收藏记录
		newcollection := model.ClubCollection{
			Uid: uid,
			Cid: cid,
		}

		err2 := DB.Create(&newcollection).Error
		// 将收藏记录保存到数据库
		if err2 != nil {
			response.Response(ctx, http.StatusInternalServerError, 400, nil, "收藏社团失败，请重试")
			log.Println("err2", err2)
			return
		}
		response.Response(ctx, http.StatusOK, 200, nil, "收藏社团成功")
	} else {
		// 删除收藏记录
		var collection model.ClubCollection
		err3 := DB.Where("uid=? AND cid=?", uid, cid).Delete(&collection).Error
		if err3 != nil {
			response.Response(ctx, http.StatusInternalServerError, 400, nil, "取消收藏失败，请重试")
			log.Println("err3", err3)
			return
		}
		response.Response(ctx, http.StatusOK, 200, nil, "取消收藏社团成功")
	}
}
